package cn.xiaou.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import cn.xiaou.domain.User;
import cn.xiaou.draw.MyJButtom;
import cn.xiaou.draw.MyTextFieldBorder;
import cn.xiaou.service.UserService;
import cn.xiaou.service.impl.UserServiceImpl;
import cn.xiaou.utils.ImageManagerUtils;

public class RechargeView extends JFrame implements ActionListener{
	private static final long serialVersionUID = 1L;
	private User user;
	private JFrame mainFrame;
	private JTextField moneyText;
	private JButton recharge,back;
	private UserService userService = new UserServiceImpl();
	public RechargeView(User user,JFrame mainFrame)  {
		this.mainFrame=mainFrame;
		this.user = user;
		setSize(400, 180);
		setUndecorated(true);
//		setAlwaysOnTop(true);
//	    setBackground(new Color(0,0,0,0));
		
		setIconImage(getToolkit().getImage(ImageManagerUtils.icoUrl));
		ImageIcon background = new ImageIcon(ImageManagerUtils.getRoot()+"\\img\\RechargeBg.png");
		 JLabel bg = new JLabel(background);
		 bg.setBounds(0, 0, background.getIconWidth(),background.getIconHeight());
		 JPanel panel = (JPanel) getContentPane();  
		 //设置透明
		 panel.setOpaque(false);
		 //将背景图的label置于窗体的底层
		 getLayeredPane().add(bg, new Integer(Integer.MIN_VALUE)); 
		panel.setLayout(null);
		panel.setSize(400, 200);
		JLabel label = new JLabel("充值金额");
		label.setBounds(60, 60, 60, 30);
		panel.add(label);
		setLocationRelativeTo(null);
		
	    
	    
		moneyText = new JTextField();
		moneyText.setBorder(new MyTextFieldBorder());
		moneyText.setOpaque(false);
		moneyText.setBounds(130, 60, 120, 30);
		panel.add(moneyText);
		
		recharge = new MyJButtom(ImageManagerUtils.getRoot()+"\\img\\rechargeb1.png");
		recharge.setBounds(150, 100, 62, 70);
		recharge.addActionListener(this);
		panel.add(recharge);
		
		back = new MyJButtom(ImageManagerUtils.exit);
		back.setBounds(370, 150, 30, 30);
		back.addActionListener(this);
		panel.add(back);
		
		setVisible(true);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == recharge) {
			if("".equals(moneyText.getText())) {
				Object[] options = { "返回"}; 
				JOptionPane.showOptionDialog(null, "充值金额为空", "警告", 
						JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, 
						null, options,options[0]); 
				return;
			}
			Integer money;
			try {
				money = Integer.valueOf(moneyText.getText());
			}catch(NumberFormatException ex) {
				Object[] options = { "返回"}; 
				JOptionPane.showOptionDialog(null, "不能输入非数字", "警告", 
						JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, 
						null, options,options[0]); 
				return;
			}
			if(money>0) {
				Object[] options = { "返回"}; 
				JOptionPane.showOptionDialog(null, "输入的充值金额不正确", "警告", 
						JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, 
						null, options,options[0]); 
				return;
			}
			Boolean state = userService.Recharge(user,money);
			if(state) {
				Object[] options = { "返回"}; 
				JOptionPane.showOptionDialog(null, "充值成功", "警告", 
						JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, 
						null, options,options[0]); 
				mainFrame.setEnabled(true);
				dispose();
			}else {
				Object[] options = { "返回"}; 
				JOptionPane.showOptionDialog(null, "充值失败", "警告", 
						JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, 
						null, options,options[0]);
				mainFrame.setEnabled(true);
				dispose();
			}
			
		}else if(e.getSource()==back) {
//			new MainFrame(null);
			mainFrame.setEnabled(true);
			dispose();
		}
		
	}
	
	

}
